using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

/// <summary>
/// CC3表情纠正数据
/// </summary>
[System.Serializable]
public class CC3BlendShapesFixData
{
    [Header("纠正步骤描述")]
    public string fixStepDesc; //纠正步骤描述

    [Header("要纠正的节点")]
    public CC3SubMeshHeadType fixType; //要纠正的节点

    [Header("对应骨骼的名称")]
    public string fixBoneName;

    [Header("对应要被纠正表情的名称")]
    public List<string> fixBlendShapesNamesList;

    [Header("纠正偏移量 (Local)")]
    public FixOffset fixOffset;

    [System.Serializable]
    public class FixOffset
    {
        [Header("位置偏移量 (Local)")]
        public Vector3 positionOffset; //位置偏移量

        [Header("旋转偏移量 (Local)")]
        public Vector3 rotationOffset; //旋转偏移量
    }
}

[CreateAssetMenu(menuName = "Presence/CC3BlendShapesFixDataAsset")]
public class CC3BlendShapesFixDataAsset : ScriptableObject
{
    public static void Create()
    {
        var asset = CreateInstance<CC3BlendShapesFixDataAsset>();
        AssetDatabase.Refresh();
    }

    [Header("对应角色")]
    public string characterName;

    [Header("纠正数据列表")]
    public List<CC3BlendShapesFixData> fixDatasList;

    public void AddStep()
    {
        if (fixDatasList == null)
        {
            fixDatasList = new List<CC3BlendShapesFixData>();
        }

        CC3BlendShapesFixData data = new CC3BlendShapesFixData();
        Transform selection = Selection.activeTransform;
        if (selection == null)
        {
            return;
        }

        data.fixBoneName = selection.name;
        data.fixOffset = GetFixOffset(selection.name);
        fixDatasList.Add(data);
    }

    /// <summary>
    /// 根据骨骼名获取到偏移量
    /// </summary>
    private CC3BlendShapesFixData.FixOffset GetFixOffset(string boneName)
    {
        var bone = SkinnProCustomWindow.Instance.GetInspector().GetBone(boneName);
        if (bone == null)
        {
            return null;
        }

        CC3BlendShapesFixData.FixOffset fixOffset = new CC3BlendShapesFixData.FixOffset();

        Vector3 targetPosition = bone.localPosition;
        Vector3 targetRotation = bone.GetRotationOnInspector(); //获取到面板上Rotation的值 被纠正后的值

        SkinnProCustomWindow.Instance.GetInspector().ResetBonesInfo();
        Vector3 previousPosition = bone.localPosition;
        Vector3 previousRotation = bone.GetRotationOnInspector(); //获取到面板上Rotation的值 骨骼默认值

        fixOffset.positionOffset = targetPosition - previousPosition;
        fixOffset.rotationOffset = targetRotation - previousRotation;

        return fixOffset;
    }
}

[CustomEditor(typeof(CC3BlendShapesFixDataAsset), true), CanEditMultipleObjects]
public class CC3BlendShapesFixDataAssetEditor : Editor
{
    private SerializedProperty characterProperty;
    private SerializedProperty fixDatasListProperty;

    private void OnEnable()
    {
        characterProperty = serializedObject.FindProperty("characterName");
        fixDatasListProperty = serializedObject.FindProperty("fixDatasList");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        GUILayout.Space(10);
        if (GUILayout.Button("保存"))
        {
            AssetDatabase.SaveAssets();
        }

        EditorGUILayout.PropertyField(characterProperty);
        GUILayout.Space(10);
        if (GUILayout.Button("添加纠正步骤"))
        {
            CC3BlendShapesFixDataAsset asset = (CC3BlendShapesFixDataAsset)fixDatasListProperty.serializedObject.targetObject;
            asset.AddStep();

            EditorUtility.SetDirty(asset);
        }

        EditorGUILayout.PropertyField(fixDatasListProperty);

        serializedObject.ApplyModifiedProperties();
    }
}